package ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class MainWindow extends JFrame implements ActionListener {

	protected JButton continueButton;
	protected DirectorySelectPanel dirSelPanel;
	protected JFileChooser fileChooser;
	protected File file;
	
	public MainWindow ()
	{
		mainWindow();
	}
	private void mainWindow()
	{
		instantiate();
	}
	
	private void instantiate()
	{
		String s = null;
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.dirSelPanel = new DirectorySelectPanel(this);
		this.fileChooser = new JFileChooser(s);
		fileChooser.setMultiSelectionEnabled(false); //TODO:: Change later to allow multiple selection
		fileChooser.setFileSelectionMode(fileChooser.DIRECTORIES_ONLY);
		
		this.continueButton = new JButton("Continue");
		continueButton.addActionListener(this);
		this.setLayout(new BorderLayout());
		this.add(dirSelPanel, BorderLayout.NORTH);
		this.setMinimumSize(new Dimension(400,200));
		
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getSource() == continueButton)
		{
			
		}
	}
	
	/*
	 * This is the function call that says the the Browse button has been pressed.
	 */
	public void callBrowsePressed() {
		// TODO Auto-generated method stub
		int returnVal = fileChooser.showOpenDialog(dirSelPanel);
		JFrame errFrame = new JFrame();
		switch (returnVal)
		{
			case (JFileChooser.APPROVE_OPTION):
				file = fileChooser.getSelectedFile();
				dirSelPanel.setValidDestination();
				break;
			case (JFileChooser.CANCEL_OPTION):
			case (JFileChooser.ERROR_OPTION):
				file = null;
				errFrame.add(new JTextField("Directory Not Selected"));
				break;
			default:
				errFrame.add(new JTextField("Unknown Return val"));
				break;
		}
		if (dirSelPanel.isValidDestination())
		{
			dirSelPanel.updateTextBox(file.getAbsolutePath());
		}
	}

}
